﻿using System.Collections.Generic;
using System.Linq;
using AngularStart1.Models;

namespace AngularStart1.Data
{
    public interface ITodoRepository
    {
        Todo Get(int id);
        Todo Add(Todo todo);
        void Remove(int id);
        void Complete(int id);
        List<Todo> List();
        Todo Update(Todo todo);
        //List<TodoPriority> Priorities();
    }

    public class InMemoryTodoRepository : ITodoRepository
    {
        // In memory todos
        private static List<Todo> _Todos;

        public InMemoryTodoRepository()
        {
            if (_Todos == null)
            {
                _Todos = new List<Todo>();
                _Todos.Add(new Todo()
                {
                    TodoId = 1,
                    Completed = false,
                    Description = "Not expanding in screen size.",
                });

                _Todos.Add(new Todo()
                {
                    TodoId = 2,
                    Completed = false,
                    Description = "Fix the new priority option...",
                });
            }
        }

        public Todo Get(int id)
        {
            var todoToFind = FindTodo(id);
            return todoToFind;
        }

        public Todo Add(Todo todo)
        {
            if (todo != null)
            {
                int todoId = _Todos.Count + 1;
                todo.TodoId = todoId;
                _Todos.Add(todo);
                return todo;
            }
            return null;
        }

        public void Remove(int id)
        {
            var todoToFind = FindTodo(id);
            if (_Todos.Contains(todoToFind))
            {
                _Todos.Remove(todoToFind);
            }
        }

        private static Todo FindTodo(int id)
        {
            return (from t in _Todos
                    where t.TodoId == id
                    select t).FirstOrDefault();
        }

        public void Complete(int id)
        {
            var todoToComplete = FindTodo(id);
            if (todoToComplete != null)
            {
                todoToComplete.Completed = true;
            }
        }

        public List<Todo> List()
        {
            return _Todos;
        }

        public Todo Update(Todo todo)
        {
            var todoToUpdate = FindTodo(todo.TodoId.GetValueOrDefault());
            if (todoToUpdate != null)
            {
                todoToUpdate.Completed = todo.Completed;
                todoToUpdate.Description = todo.Description;
            }
            return todoToUpdate;
        }

        private string PriorityDescr(int? id)
        {
            var description = string.Empty;
            var priority = Priorities().FirstOrDefault(p => p.Id == id.GetValueOrDefault());
            if (priority != null)
                description = priority.Description;
            return description;
        }

        public List<TodoPriority> Priorities()
        {
            var items = new List<TodoPriority>();
            items.Add(new TodoPriority
            {
                Description = "Low",
                Id = 1
            });
            items.Add(new TodoPriority
            {
                Description = "Normal",
                Id = 2
            });
            items.Add(new TodoPriority
            {
                Description = "High",
                Id = 3
            });

            return items;
        }

        public void Delete(int todiId)
        {
            var todoToDelete = FindTodo(todiId);
            if (todoToDelete != null)
            {
                _Todos.Remove(todoToDelete);
            }
        }
    }
}